# -*- coding: utf-8 -*-
"""

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

发行版特定检测模块
包含各种Linux发行版特定的配置和服务检测功能
"""

from core.utils import SystemInfo, safe_print, safe_str
import os
import subprocess
import re

# 导入安全工具
try:
    from core.security_utils import SecureCommandExecutor, SecurityValidator
    SECURITY_UTILS_AVAILABLE = True
except ImportError:
    SECURITY_UTILS_AVAILABLE = False
    safe_print("安全工具模块不可用，将使用基本安全检查")

def safe_subprocess_run(command, **kwargs):
    """安全的subprocess执行函数"""
    if SECURITY_UTILS_AVAILABLE:
        # 验证命令安全性
        if isinstance(command, list):
            cmd_str = ' '.join(command)
        else:
            cmd_str = command
            
        if not SecurityValidator.validate_command(cmd_str):
            safe_print("命令不安全: {}".format(cmd_str))
            return None
        
        # 使用安全执行器
        result = SecureCommandExecutor.execute_safe_command(
            cmd_str,
            timeout=kwargs.get('timeout', 10),
            allowed_commands=['which', 'systemctl', 'dpkg', 'rpm', 'zypper', 'pacman', 'apk', 'yum', 'dnf', 'ldd']
        )
        
        # 转换为subprocess.CompletedProcess格式
        class SafeResult:
            def __init__(self, returncode, stdout, stderr):
                self.returncode = returncode
                self.stdout = stdout.encode('utf-8') if isinstance(stdout, str) else stdout
                self.stderr = stderr.encode('utf-8') if isinstance(stderr, str) else stderr
        
        return SafeResult(
            result.get('returncode', 1),
            result.get('stdout', ''),
            result.get('stderr', '')
        )
    else:
        # 回退到原始方法
        return subprocess.run(command, **kwargs)





def check_debian_specific():
    """检测Debian特定配置"""
    safe_print("=== Debian特定配置检测 ===")
    
    # 检测Debian版本信息
    debian_version_files = [
        '/etc/debian_version',
        '/etc/os-release',
        '/etc/lsb-release'
    ]
    
    for version_file in debian_version_files:
        if os.path.exists(version_file):
            safe_print("发现Debian版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Debian特定配置文件
    debian_configs = [
        '/etc/apt/sources.list',
        '/etc/apt/sources.list.d/',
        '/etc/dpkg/dpkg.cfg',
        '/etc/alternatives/',
        '/var/lib/dpkg/status'
    ]
    
    for config in debian_configs:
        if os.path.exists(config):
            safe_print("发现Debian配置: {}".format(config))
    
    # 检测Debian特定服务
    debian_services = [
        'systemd',
        'cron',
        'rsyslog',
        'ssh',
        'networking',
        'ufw'
    ]
    
    for service in debian_services:
        try:
            result = subprocess.run(['systemctl', 'is-active', service], 
                                  capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("Debian服务 {}: 运行中".format(service))
            else:
                safe_print("Debian服务 {}: 未运行".format(service))
        except Exception:
            pass
    
    # 检测包管理工具
    package_tools = ['apt', 'apt-get', 'dpkg', 'aptitude']
    for tool in package_tools:
        try:
            result = safe_subprocess_run(['which', tool], capture_output=True, text=True, timeout=5)
            if result and result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = safe_subprocess_run(['dpkg', '-l'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result and result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for package in packages.split('\n'):
                if package.startswith('ii'):
                    parts = package.split()
                    if len(parts) >= 2:
                        package_name = parts[1]
                        if any(keyword in package_name.lower() for keyword in ['dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                            dev_packages.append(package_name)
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = safe_subprocess_run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result and result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_fedora_specific():
    """检测Fedora特定配置"""
    safe_print("=== Fedora特定配置检测 ===")
    
    # 检测Fedora版本信息
    fedora_version_files = [
        '/etc/fedora-release',
        '/etc/redhat-release',
        '/etc/os-release'
    ]
    
    for version_file in fedora_version_files:
        if os.path.exists(version_file):
            safe_print("发现Fedora版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Fedora特定配置文件
    fedora_configs = [
        '/etc/yum.conf',
        '/etc/dnf/dnf.conf',
        '/etc/yum.repos.d/',
        '/etc/rpm/macros',
        '/var/lib/rpm/'
    ]
    
    for config in fedora_configs:
        if os.path.exists(config):
            safe_print("发现Fedora配置: {}".format(config))
    
    # 检测Fedora特定服务
    fedora_services = [
        'systemd',
        'crond',
        'rsyslog',
        'sshd',
        'firewalld',
        'NetworkManager'
    ]
    
    for service in fedora_services:
        try:
            result = subprocess.run(['systemctl', 'is-active', service], 
                                  capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("Fedora服务 {}: 运行中".format(service))
            else:
                safe_print("Fedora服务 {}: 未运行".format(service))
        except Exception:
            pass
    
    # 检测包管理工具
    package_tools = ['dnf', 'rpm', 'yum']
    for tool in package_tools:
        try:
            result = subprocess.run(['which', tool], capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['rpm', '-qa'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for package in packages.split('\n'):
                if package.strip():
                    if any(keyword in package.lower() for keyword in ['devel', 'dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                        dev_packages.append(package.strip())
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_suse_specific():
    """检测SUSE特定配置"""
    safe_print("=== SUSE特定配置检测 ===")
    
    # 检测SUSE版本信息
    suse_version_files = [
        '/etc/SuSE-release',
        '/etc/os-release',
        '/etc/SUSE-brand'
    ]
    
    for version_file in suse_version_files:
        if os.path.exists(version_file):
            safe_print("发现SUSE版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测SUSE特定配置文件
    suse_configs = [
        '/etc/zypp/zypp.conf',
        '/etc/zypp/repos.d/',
        '/etc/sysconfig/',
        '/etc/YaST2/',
        '/var/lib/zypp/'
    ]
    
    for config in suse_configs:
        if os.path.exists(config):
            safe_print("发现SUSE配置: {}".format(config))
    
    # 检测SUSE特定服务
    suse_services = [
        'systemd',
        'cron',
        'rsyslog',
        'sshd',
        'firewalld',
        'NetworkManager',
        'apparmor'
    ]
    
    for service in suse_services:
        try:
            result = subprocess.run(['systemctl', 'is-active', service], 
                                  capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("SUSE服务 {}: 运行中".format(service))
            else:
                safe_print("SUSE服务 {}: 未运行".format(service))
        except Exception:
            pass
    
    # 检测包管理工具
    package_tools = ['zypper', 'rpm', 'yast', 'yast2']
    for tool in package_tools:
        try:
            result = subprocess.run(['which', tool], capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['zypper', 'se', '-i'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for line in packages.split('\n'):
                if '|' in line and 'i' in line.split('|')[0]:
                    parts = line.split('|')
                    if len(parts) >= 2:
                        package_name = parts[1].strip()
                        if any(keyword in package_name.lower() for keyword in ['devel', 'dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                            dev_packages.append(package_name)
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_alpine_specific():
    """检测Alpine特定配置"""
    safe_print("=== Alpine特定配置检测 ===")
    
    # 检测Alpine版本信息
    alpine_version_files = [
        '/etc/alpine-release',
        '/etc/os-release'
    ]
    
    for version_file in alpine_version_files:
        if os.path.exists(version_file):
            safe_print("发现Alpine版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Alpine特定配置文件
    alpine_configs = [
        '/etc/apk/repositories',
        '/etc/apk/world',
        '/etc/runlevels/',
        '/etc/conf.d/',
        '/etc/init.d/'
    ]
    
    for config in alpine_configs:
        if os.path.exists(config):
            safe_print("发现Alpine配置: {}".format(config))
    
    # 检测Alpine特定服务
    alpine_services = [
        'sshd',
        'crond',
        'syslog',
        'networking',
        'ntpd'
    ]
    
    for service in alpine_services:
        try:
            result = subprocess.run(['rc-service', service, 'status'], 
                                  capture_output=True, text=True, timeout=5)
            if 'started' in result.stdout:
                safe_print("Alpine服务 {}: 运行中".format(service))
            else:
                safe_print("Alpine服务 {}: 未运行".format(service))
        except Exception:
            pass
    
    # 检测包管理工具
    package_tools = ['apk', 'rc-service', 'rc-status', 'rc-update']
    for tool in package_tools:
        try:
            result = subprocess.run(['which', tool], capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测musl libc
    try:
        result = subprocess.run(['ldd', '--version'], capture_output=True, text=True, timeout=5)
        if 'musl' in result.stdout:
            safe_print("检测到musl libc")
    except Exception:
        pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['apk', 'list', '-I'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for line in packages.split('\n'):
                if line.strip():
                    package_name = line.split()[0] if line.split() else ""
                    if any(keyword in package_name.lower() for keyword in ['dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                        dev_packages.append(package_name)
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    # 检查libc版本
    try:
        result = subprocess.run(['ldd', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            libc_info = result.stdout.decode('utf-8').split('\n')[0]
            available_tools.append("libc: {}".format(libc_info))
    except:
        pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_alibaba_specific():
    """检测阿里云Linux特定配置"""
    safe_print("=== 阿里云Linux特定配置检测 ===")
    
    # 检测阿里云版本信息
    alibaba_version_files = [
        '/etc/alinux-release',
        '/etc/alios-release',
        '/etc/os-release'
    ]
    
    for version_file in alibaba_version_files:
        if os.path.exists(version_file):
            safe_print("发现阿里云版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测阿里云特定配置文件
    alibaba_configs = [
        '/etc/alibaba-cloud/',
        '/etc/cloud/',
        '/var/lib/cloud/',
        '/etc/yum.repos.d/alinux.repo',
        '/etc/yum.repos.d/alibaba-cloud.repo'
    ]
    
    for config in alibaba_configs:
        if os.path.exists(config):
            safe_print("发现阿里云配置: {}".format(config))
    
    # 检测阿里云特定服务
    alibaba_services = [
        'systemd',
        'sshd',
        'crond',
        'rsyslog',
        'cloud-init',
        'cloud-config',
        'cloud-final'
    ]
    
    for service in alibaba_services:
        try:
            result = subprocess.run(['systemctl', 'is-active', service], 
                                  capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("阿里云服务 {}: 运行中".format(service))
            else:
                safe_print("阿里云服务 {}: 未运行".format(service))
        except Exception:
            pass


def check_anolis_specific():
    """检测Anolis OS特定配置"""
    safe_print("=== Anolis OS特定配置检测 ===")
    
    # 检测Anolis版本信息
    anolis_version_files = [
        '/etc/anolis-release',
        '/etc/os-release'
    ]
    
    for version_file in anolis_version_files:
        if os.path.exists(version_file):
            safe_print("发现Anolis版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Anolis特定配置文件
    anolis_configs = [
        '/etc/yum.repos.d/AnolisOS.repo',
        '/etc/anolis/',
        '/var/lib/anolis/'
    ]
    
    for config in anolis_configs:
        if os.path.exists(config):
            safe_print("发现Anolis配置: {}".format(config))


def check_arch_specific():
    """检测Arch Linux特定配置"""
    safe_print("=== Arch Linux特定配置检测 ===")
    
    # 检测Arch版本信息
    arch_version_files = [
        '/etc/arch-release',
        '/etc/os-release'
    ]
    
    for version_file in arch_version_files:
        if os.path.exists(version_file):
            safe_print("发现Arch版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Arch特定配置文件
    arch_configs = [
        '/etc/pacman.conf',
        '/etc/pacman.d/',
        '/var/lib/pacman/',
        '/etc/makepkg.conf'
    ]
    
    for config in arch_configs:
        if os.path.exists(config):
            safe_print("发现Arch配置: {}".format(config))
    
    # 检测包管理工具
    package_tools = ['pacman', 'makepkg', 'yay', 'paru']
    for tool in package_tools:
        try:
            result = subprocess.run(['which', tool], capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['pacman', '-Q'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for line in packages.split('\n'):
                if line.strip():
                    package_name = line.split()[0] if line.split() else ""
                    if any(keyword in package_name.lower() for keyword in ['dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                        dev_packages.append(package_name)
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    # 检查libc版本
    try:
        result = subprocess.run(['ldd', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            libc_info = result.stdout.decode('utf-8').split('\n')[0]
            available_tools.append("libc: {}".format(libc_info))
    except:
        pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_gentoo_specific():
    """检测Gentoo特定配置"""
    safe_print("=== Gentoo特定配置检测 ===")
    
    # 检测Gentoo版本信息
    gentoo_version_files = [
        '/etc/gentoo-release',
        '/etc/os-release'
    ]
    
    for version_file in gentoo_version_files:
        if os.path.exists(version_file):
            safe_print("发现Gentoo版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测Gentoo特定配置文件
    gentoo_configs = [
        '/etc/portage/',
        '/var/db/repos/gentoo/',
        '/etc/make.conf',
        '/var/lib/portage/'
    ]
    
    for config in gentoo_configs:
        if os.path.exists(config):
            safe_print("发现Gentoo配置: {}".format(config))
    
    # 检测包管理工具
    package_tools = ['emerge', 'portageq', 'equery', 'eix']
    for tool in package_tools:
        try:
            result = subprocess.run(['which', tool], capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("包管理工具 {}: 可用".format(tool))
        except Exception:
            pass
    
    # 检测开发工具和环境
    available_tools = []
    
    try:
        result = subprocess.run(['emerge', '--info'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            info = result.stdout.decode('utf-8')
            # 解析Portage信息
            for line in info.split('\n'):
                if 'CHOST=' in line:
                    available_tools.append("CHOST: {}".format(line.split('=')[1].strip()))
                elif 'USE=' in line:
                    use_flags = line.split('=')[1].strip()
                    if any(flag in use_flags for flag in ['dev', 'build', 'gcc']):
                        available_tools.append("Development USE flags enabled")
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_centos_specific():
    """检测CentOS特定配置"""
    safe_print("=== CentOS特定配置检测 ===")
    
    # 检测CentOS版本信息
    centos_version_files = [
        '/etc/centos-release',
        '/etc/redhat-release',
        '/etc/os-release'
    ]
    
    for version_file in centos_version_files:
        if os.path.exists(version_file):
            safe_print("发现CentOS版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测CentOS特定配置文件
    centos_configs = [
        '/etc/yum.conf',
        '/etc/yum.repos.d/',
        '/etc/sysconfig/',
        '/var/lib/rpm/'
    ]
    
    for config in centos_configs:
        if os.path.exists(config):
            safe_print("发现CentOS配置: {}".format(config))
    
    # 检测CentOS特定服务
    centos_services = [
        'systemd',
        'crond',
        'rsyslog',
        'sshd',
        'firewalld',
        'NetworkManager'
    ]
    
    for service in centos_services:
        try:
            result = subprocess.run(['systemctl', 'is-active', service], 
                                  capture_output=True, text=True, timeout=5)
            if result.returncode == 0:
                safe_print("CentOS服务 {}: 运行中".format(service))
            else:
                safe_print("CentOS服务 {}: 未运行".format(service))
        except Exception:
            pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['rpm', '-qa'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for package in packages.split('\n'):
                if package.strip():
                    if any(keyword in package.lower() for keyword in ['devel', 'dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                        dev_packages.append(package.strip())
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))


def check_rhel_specific():
    """检测RHEL特定配置"""
    safe_print("=== RHEL特定配置检测 ===")
    
    # 检测RHEL版本信息
    rhel_version_files = [
        '/etc/redhat-release',
        '/etc/os-release',
        '/etc/system-release'
    ]
    
    for version_file in rhel_version_files:
        if os.path.exists(version_file):
            safe_print("发现RHEL版本文件: {}".format(version_file))
            try:
                with open(version_file, 'r') as f:
                    content = f.read().strip()
                    safe_print("内容: {}".format(content))
            except Exception as e:
                safe_print("读取失败: {}".format(e))
    
    # 检测RHEL特定配置文件
    rhel_configs = [
        '/etc/yum.conf',
        '/etc/yum.repos.d/',
        '/etc/sysconfig/',
        '/var/lib/rpm/',
        '/etc/subscription-manager/'
    ]
    
    for config in rhel_configs:
        if os.path.exists(config):
            safe_print("发现RHEL配置: {}".format(config))
    
    # 检测安全特性
    security_features = []
    
    # 检测SELinux状态
    try:
        result = subprocess.run(['getenforce'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            selinux_status = result.stdout.decode('utf-8').strip()
            security_features.append("SELinux: {}".format(selinux_status))
    except:
        pass
    
    # 检测开发包和工具
    dev_packages = []
    available_tools = []
    
    try:
        result = subprocess.run(['rpm', '-qa'], 
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
        if result.returncode == 0:
            packages = result.stdout.decode('utf-8')
            for package in packages.split('\n'):
                if package.strip():
                    if any(keyword in package.lower() for keyword in ['devel', 'dev', 'build', 'gcc', 'make', 'cmake', 'git']):
                        dev_packages.append(package.strip())
    except:
        pass
    
    # 检查开发工具
    dev_tools = ['gcc', 'g++', 'make', 'cmake', 'git', 'vim', 'nano']
    for tool in dev_tools:
        try:
            result = subprocess.run(['which', tool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
            if result.returncode == 0:
                available_tools.append(tool)
        except:
            pass
    
    if security_features:
        safe_print("安全特性: {}".format(', '.join(security_features)))
    if dev_packages:
        safe_print("发现开发包: {}{}".format(', '.join(dev_packages[:10]), '...' if len(dev_packages) > 10 else ''))
    if available_tools:
        safe_print("可用开发工具: {}".format(', '.join(available_tools)))